/**
* 图片裁切JS文件
* author xiezhihui 2014-01-06 09:39:54
*/


var cut_img;
(function (undefined){

	cut_img = {
		idname:'裁剪',
		img_obj:null,
		id:'img_span',
		parent_function:'cut_refresh',//裁图调取回调喊出
		img_width:400,
		img_height:300,
		ratio:'4:3',//图片比例默认4;3
		img_src:false,//自定义图片路径
		box:null,
		idcss:{
			'width':'35px',
			'position':'absolute',
			'left':'2px',
			'top':'150px'
		}
	}
	cut_img.init=function (div_obj){

		cut_img.img_obj = new Image();
		cut_img.img_obj.src = div_obj.find('img').attr('src') && cut_img.img_src ? cut_img.img_src: div_obj.find('img').attr('src');
		if(cut_img.img_src){
			$('#'+cut_img.id+'').remove();
		}
		div_obj.append("<span id='"+cut_img.id+"' style='position:absolute;left:2px;top:150px;width:35px;'onclick='cut_img.cut_start(\""+cut_img.img_obj.src+"\")' >裁剪</span>");

	}
	cut_img._init=function (config){
		var css='style="';
		for(var i in config){
			if(typeof config[i] == 'object' && i=='idcss'){

				for (var k in config[i]){
					cut_img[i][k]=config[i][k];
				}
			}else{
				cut_img[i]=config[i];
			}

		}
		for (var c in cut_img.idcss){

			css+=c+':'+cut_img.idcss[c]+';';
		}
		css+='"';
		cut_img.img_obj = new Image();
		cut_img.img_obj.src = cut_img.box.find('img').attr('src') && cut_img.img_src ? cut_img.img_src: cut_img.box.find('img').attr('src');
		//if(cut_img.img_src){
			//alert(cut_img.id);
			$('#'+cut_img.id+'').remove();
		//}
		cut_img.box.append("<span id='"+cut_img.id+"' "+css+" onclick='cut_img.cut_start(\""+cut_img.img_obj.src+"\")' >"+cut_img.idname+"</span>");
		return this;
	}
	/**
	* 裁图开始 传入的是img src
	*/
	cut_img.cut_start=function (src){
		cut_img.dialog(src)
	}
	cut_img.dialog = function (src,ratio){

		dialog.iframe({
			title:'图片裁剪',
			is_resize:false,
			top:{
				is_show:true,
				btn_max:false,
				btn_min:false,
				btn_close:false,
				height:33,
				is_move:false
			},
			is_scroll:false,
			min_width:600,
			min_height:550,
			is_center:true,
			is_lock_back:true,
			content:'/album/cut_img?callback=get_img_info&ratio='+cut_img.ratio+'&src='+src+'&parent_function='+cut_img.parent_function,
			name:'dialog_cut'
		});
	}
	//自动调整图片宽高大小
	cut_img.autoRsize = function (maxWidth,maxHeight,objImg){
		var img = new Image();
		img.src = objImg.src;
		var hRatio;
		var wRatio;
		var Ratio = 1;
		var w = img.width;
		var h = img.height;
		wRatio = maxWidth / w;
		hRatio = maxHeight / h;
		if (maxWidth ==0 && maxHeight==0){
			Ratio = 1;
		}else if (maxWidth==0){

			if(hRatio<1) Ratio = hRatio;

		}else if (maxHeight==0){

			if (wRatio<1) Ratio = wRatio;

		}else if (wRatio<1 || hRatio<1){

			Ratio = (wRatio<=hRatio?wRatio:hRatio);
		}
		if (Ratio<1){
			w = w * Ratio;
			h = h * Ratio;
		}
		img_height = h;
		img_width = w;
		objImg.height = h;
		objImg.width = w;
	}
})()
